package baseclass.b_stackQueue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @date 2020/2/27 19:37
 */
public class Code05_QueueToStack {
    private Queue<Integer> dataQueue;
    private Queue<Integer> helpQueue;


    public Code05_QueueToStack() {
        this.dataQueue = new LinkedList<>();
        this.helpQueue = new LinkedList<>();
    }
    public void push(int e){
        dataQueue.add(e);
    }
    public int pop(){
        if(dataQueue.size() == 0)
            throw new RuntimeException("empty");
        while (dataQueue.size() > 1){
            helpQueue.add(dataQueue.poll());
        }
        int res = dataQueue.poll();
        swap();
        return res;
    }

    private void swap() {
        Queue<Integer> temp = dataQueue;
        dataQueue = helpQueue;
        helpQueue = temp;
    }
    public boolean isEmpty(){
        return dataQueue.isEmpty() && helpQueue.isEmpty();
    }

    public static void main(String[] args) {
        Code05_QueueToStack stack = new Code05_QueueToStack();
        for (int i = 1; i < 11; i++) {
            stack.push(i);
        }
        while (!stack.isEmpty())
            System.out.println(stack.pop());
    }
}
